﻿using Microsoft.AspNet.Identity.EntityFramework;
using SGIMail.DAL;
using SGIMail.Models;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SGIMail.DAL
{
    public class ApplicationUserStore<T> : UserStore<ApplicationUser>
    {
        public ApplicationDbContext DbContext { get; set; }

        public ApplicationUserStore(ApplicationDbContext context)
            :base(context)
        {
            //DbContext = new ApplicationDbContext();
            DbContext = userStore.Context as ApplicationDbContext;
        }

        UserStore<IdentityUser> userStore = new UserStore<IdentityUser>(new ApplicationDbContext());

        #region OVERRIDE

        public override Task CreateAsync(ApplicationUser user)
        {
            var context = userStore.Context as ApplicationDbContext;
            context.Users.Add(user);
            context.Configuration.ValidateOnSaveEnabled = false;
            return context.SaveChangesAsync();
        }

        public override Task DeleteAsync(ApplicationUser user)
        {
            var context = userStore.Context as ApplicationDbContext;
            context.Users.Remove(user);
            context.Configuration.ValidateOnSaveEnabled = false;
            return context.SaveChangesAsync();
        }

        public override Task<ApplicationUser> FindAsync(Microsoft.AspNet.Identity.UserLoginInfo login)
        {
            return base.FindAsync(login);

            //var query = (from q in DbContext.Users
            //                 .Where(x => x.Logins.Where(x2 => x2.ProviderKey == login.ProviderKey) == login.ProviderKey)
            //             select q);

            //var query = (from q in DbContext.Users
            //            select q.Logins).ToList();

            //foreach (var item in query)
            //{
            //    foreach (var lol in item)
            //    {
            //        if (lol.ProviderKey == login.ProviderKey)
            //            // Nudå.
            //    }
            //}
        }

        public override Task<ApplicationUser> FindByIdAsync(string userId)
        {
            var context = userStore.Context as ApplicationDbContext;
            return context.Users.Where(u => u.Id.ToLower() == userId.ToLower()).FirstOrDefaultAsync();
        }

        public override Task<ApplicationUser> FindByNameAsync(string userName)
        {
            //var context = userStore.Context as ApplicationDbContext;
            //return context.Users.Where(u => u.UserName.ToLower() == userName.ToLower()).FirstOrDefaultAsync();
            return DbContext.Users.Where(u => u.UserName.ToLower() == userName.ToLower()).FirstOrDefaultAsync();
        }

        public override Task UpdateAsync(ApplicationUser user)
        {
            //var context = userStore.Context as ApplicationDbContext;
            //context.Users.Attach(user);
            //context.Entry(user).State = EntityState.Modified;
            //context.Configuration.ValidateOnSaveEnabled = false;
            //return context.SaveChangesAsync();

            //var context = userStore.Context as ApplicationDbContext;
            DbContext.Users.Attach(user);
            DbContext.Entry(user).State = EntityState.Modified;
            DbContext.Configuration.ValidateOnSaveEnabled = false;
            return DbContext.SaveChangesAsync();
        }

        //public void Dispose()
        //{
        //    userStore.Dispose();
        //} 
        #endregion

        #region DB

        public void UpdateDatabase()
        {

        }

        #endregion
    }
}
